package cn.muqingfeng.bbs.ui;

import java.util.HashMap;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
import cn.muqingfeng.api.UriHelper;
import cn.muqingfeng.api.to.UserTO;
import cn.muqingfeng.bbs.R;
import cn.muqingfeng.bbs.adapter.ViewPagerAdapter;
import cn.muqingfeng.bbs.constants.Constants;
import cn.muqingfeng.bbs.fragment.Article1ListFragment;
import cn.muqingfeng.bbs.fragment.ArticleListFragment;
import cn.muqingfeng.util.AccountManager;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import cn.sharesdk.wechat.utils.WechatHelper.ShareParams;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.umeng.update.UmengUpdateAgent;
import com.viewpagerindicator.TabPageIndicator;

public class MainActivity extends SlidingFragmentActivity implements OnClickListener {
	protected ViewPager mViewPager;
	private static final int REQUEST_CODE_MAIN = 505;
	protected TabPageIndicator mIndicator;
	private SlidingMenu mSlidingMenu;
	private ImageButton mUserBtn;
	private ImageButton mMenuBtn;
	private ViewPagerAdapter mViewPagerAdapter;
	private AlertDialog mNoLoginDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setBehindContentView(R.layout.right_menu);
		setContentView(R.layout.main);
		setupViews();
		UmengUpdateAgent.setUpdateOnlyWifi(false);
		UmengUpdateAgent.update(this);
	}

	private void setupViews() {
		mSlidingMenu = getSlidingMenu();
		mSlidingMenu.setMenu(R.layout.right_menu);
		mSlidingMenu.setMode(SlidingMenu.RIGHT);
		mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
		Drawable menuBg = getResources().getDrawable(R.drawable.right_menu_bg);
		mSlidingMenu.setBehindWidth(menuBg.getIntrinsicWidth());
		mViewPager = (ViewPager) findViewById(R.id.viewpager);
		mViewPager.setOffscreenPageLimit(1);
		mViewPagerAdapter = getAdapter();
		mViewPager.setAdapter(mViewPagerAdapter);
		mIndicator = (TabPageIndicator) findViewById(R.id.indicator);
		mIndicator.setViewPager(mViewPager);
		mUserBtn = (ImageButton) findViewById(R.id.user);
		mUserBtn.setOnClickListener(this);
		mMenuBtn = (ImageButton) findViewById(R.id.menu);
		mMenuBtn.setOnClickListener(this);

		findViewById(R.id.lingting_d).setOnClickListener(this);
		findViewById(R.id.guangjie_d).setOnClickListener(this);
		findViewById(R.id.shangcheng_d).setOnClickListener(this);
		findViewById(R.id.tougao_d).setOnClickListener(this);

		findViewById(R.id.shangcheng).setOnClickListener(this);
		findViewById(R.id.lingting).setOnClickListener(this);
		findViewById(R.id.wap).setOnClickListener(this);
		findViewById(R.id.wei).setOnClickListener(this);
		findViewById(R.id.guangjie).setOnClickListener(this);
		findViewById(R.id.tougao).setOnClickListener(this);

		findViewById(R.id.weixin).setOnClickListener(this);
		findViewById(R.id.redianxinwen).setOnClickListener(this);
		findViewById(R.id.tushuotianxia).setOnClickListener(this);
		findViewById(R.id.gushihui).setOnClickListener(this);
		findViewById(R.id.lvyouduajia).setOnClickListener(this);
		findViewById(R.id.xiaoshuoyuedu).setOnClickListener(this);
		findViewById(R.id.shishangjiaju).setOnClickListener(this);
		findViewById(R.id.meishifenxiang).setOnClickListener(this);
		findViewById(R.id.meinvtupian).setOnClickListener(this);
		findViewById(R.id.qingchunwenxue).setOnClickListener(this);
		findViewById(R.id.xiaoqingxintu).setOnClickListener(this);
		findViewById(R.id.xiaohuaqutu).setOnClickListener(this);
		findViewById(R.id.xingzuowuyu).setOnClickListener(this);
		findViewById(R.id.weimeihunsha).setOnClickListener(this);
		findViewById(R.id.shamenshenghuo).setOnClickListener(this);

	}

	public ViewPagerAdapter getAdapter() {
		ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), this);

		// 首页
		Bundle homeBundle = new Bundle();
		Bundle homeParams = new Bundle();
		homeParams.putString("filter", "1");
		homeParams.putString("orderby", "1");
		homeParams.putString("_", String.valueOf(System.currentTimeMillis()));
		homeBundle.putBundle(Constants.EXTRA_BUNDLE, homeParams);
		homeBundle.putString(Constants.EXTRA_URL, UriHelper.getAllArticlesUrl());
		adapter.addPager("首页", Article1ListFragment.class.getName(), homeBundle);

		Bundle params = new Bundle();
		params.putString("orderby", "dateline");
		// Bundle ltBundle = new Bundle();
		// ltBundle.putString(Constants.EXTRA_URL,
		// "http://lingting.muqingfeng.cn/");
		// adapter.addPager("聆听", WebViewFragment.class.getName(), ltBundle);
		//
		// Bundle gjBundle = new Bundle();
		// gjBundle.putString(Constants.EXTRA_URL,
		// "http://wap.shop.muqingfeng.cn/forum.php?mod=guide&mobile=2");
		// adapter.addPager("逛街", WebViewFragment.class.getName(), gjBundle);

		Bundle xcsBundle = new Bundle();
		xcsBundle.putBundle(Constants.EXTRA_BUNDLE, params);
		xcsBundle.putString(Constants.EXTRA_URL, UriHelper.getArticlesUrl(65));
		adapter.addPager("微信分享", ArticleListFragment.class.getName(), xcsBundle);

		Bundle rdxwBundle = new Bundle();
		rdxwBundle.putBundle(Constants.EXTRA_BUNDLE, params);
		rdxwBundle.putString(Constants.EXTRA_URL, UriHelper.getArticlesUrl(187));
		adapter.addPager("热点新闻", ArticleListFragment.class.getName(), rdxwBundle);

		Bundle tstxBundle = new Bundle();
		tstxBundle.putBundle(Constants.EXTRA_BUNDLE, params);
		tstxBundle.putString(Constants.EXTRA_URL, UriHelper.getArticlesUrl(74));
		adapter.addPager("图说天下", ArticleListFragment.class.getName(), tstxBundle);

		Bundle gshBundle = new Bundle();
		gshBundle.putBundle(Constants.EXTRA_BUNDLE, params);
		gshBundle.putString(Constants.EXTRA_URL, UriHelper.getArticlesUrl(183));
		adapter.addPager("故事会", ArticleListFragment.class.getName(), gshBundle);

		Bundle lydjBundle = new Bundle();
		lydjBundle.putBundle(Constants.EXTRA_BUNDLE, params);
		lydjBundle.putString(Constants.EXTRA_URL, UriHelper.getArticlesUrl(57));
		adapter.addPager("旅游度假", ArticleListFragment.class.getName(), lydjBundle);

		Bundle xsydBundle = new Bundle();
		xsydBundle.putBundle(Constants.EXTRA_BUNDLE, params);
		xsydBundle.putString(Constants.EXTRA_URL, UriHelper.getArticlesUrl(114));
		adapter.addPager("小说阅读", ArticleListFragment.class.getName(), xsydBundle);

		Bundle ssjjBundle = new Bundle();
		ssjjBundle.putBundle(Constants.EXTRA_BUNDLE, params);
		ssjjBundle.putString(Constants.EXTRA_URL, UriHelper.getArticlesUrl(56));
		adapter.addPager("时尚家居", ArticleListFragment.class.getName(), ssjjBundle);

		Bundle msfxBundle = new Bundle();
		msfxBundle.putBundle(Constants.EXTRA_BUNDLE, params);
		msfxBundle.putString(Constants.EXTRA_URL, UriHelper.getArticlesUrl(69));
		adapter.addPager("美食分享", ArticleListFragment.class.getName(), msfxBundle);

		Bundle mntp = new Bundle();
		mntp.putBundle(Constants.EXTRA_BUNDLE, params);
		mntp.putString(Constants.EXTRA_URL, UriHelper.getArticlesUrl(59));
		adapter.addPager("美女图片", ArticleListFragment.class.getName(), mntp);

		Bundle qcwxBundle = new Bundle();
		qcwxBundle.putBundle(Constants.EXTRA_BUNDLE, params);
		qcwxBundle.putString(Constants.EXTRA_URL, UriHelper.getArticlesUrl(184));
		adapter.addPager("青春文学", ArticleListFragment.class.getName(), qcwxBundle);

		Bundle xqxtBundle = new Bundle();
		xqxtBundle.putBundle(Constants.EXTRA_BUNDLE, params);
		xqxtBundle.putString(Constants.EXTRA_URL, UriHelper.getArticlesUrl(78));
		adapter.addPager("小清新图", ArticleListFragment.class.getName(), xqxtBundle);

		Bundle xhqtBundle = new Bundle();
		xhqtBundle.putBundle(Constants.EXTRA_BUNDLE, params);
		xhqtBundle.putString(Constants.EXTRA_URL, UriHelper.getArticlesUrl(92));
		adapter.addPager("笑话趣图", ArticleListFragment.class.getName(), xhqtBundle);

		Bundle xzwyBundle = new Bundle();
		xzwyBundle.putBundle(Constants.EXTRA_BUNDLE, params);
		xzwyBundle.putString(Constants.EXTRA_URL, UriHelper.getArticlesUrl(49));
		adapter.addPager("星座物语", ArticleListFragment.class.getName(), xzwyBundle);

		Bundle wmhsBundle = new Bundle();
		wmhsBundle.putBundle(Constants.EXTRA_BUNDLE, params);
		wmhsBundle.putString(Constants.EXTRA_URL, UriHelper.getArticlesUrl(60));
		adapter.addPager("唯美婚纱", ArticleListFragment.class.getName(), wmhsBundle);

		Bundle xmshBundle = new Bundle();
		xmshBundle.putBundle(Constants.EXTRA_BUNDLE, params);
		xmshBundle.putString(Constants.EXTRA_URL, UriHelper.getArticlesUrl(135));
		adapter.addPager("厦门生活", ArticleListFragment.class.getName(), xmshBundle);

		// Bundle wsBundle = new Bundle();
		// wsBundle.putBundle(Constants.EXTRA_BUNDLE, params);
		// wsBundle.putString(Constants.EXTRA_URL,
		// UriHelper.getArticlesUrl(88));
		// wsBundle.putBoolean(Constants.EXTRA_TO_WEBDETAIL, true);
		// adapter.addPager("微视", ArticleListFragment.class.getName(),
		// wsBundle);
		//
		// Bundle gqdyBundle = new Bundle();
		// gqdyBundle.putBundle(Constants.EXTRA_BUNDLE, params);
		// gqdyBundle.putString(Constants.EXTRA_URL,
		// UriHelper.getArticlesUrl(2));
		// adapter.addPager("高清电影", ArticleListFragment.class.getName(),
		// gqdyBundle);
		//
		// Bundle scBundle = new Bundle();
		// scBundle.putString(Constants.EXTRA_URL,
		// "http://www.muqingfeng.com.cn/mobile/");
		// adapter.addPager("商城", WebViewFragment.class.getName(), scBundle);
		return adapter;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.menu:
			mSlidingMenu.toggle();
			break;
		case R.id.user: {
			Intent intent = new Intent(this, UcenterActivity.class);
			startActivity(intent);
		}
			break;
		case R.id.shangcheng: {

			Intent intent = new Intent(this, WebViewActivity.class);
			intent.putExtra(Constants.EXTRA_URL, "http://www.muqingfeng.com.cn/mobile/");
			startActivity(intent);
		}
			break;
		case R.id.wei: {

			Intent intent = new Intent(this, WebViewActivity.class);
			intent.putExtra(Constants.EXTRA_URL, "http://m.wsq.qq.com/211520395");
			startActivity(intent);
		}
			break;
		case R.id.guangjie: {

			Intent intent = new Intent(this, WebViewActivity.class);
			intent.putExtra(Constants.EXTRA_URL, "http://wap.shop.muqingfeng.cn/forum.php?mod=guide&mobile=2");
			startActivity(intent);
		}
			break;
		case R.id.wap: {
			Intent intent = new Intent(this, WebViewActivity.class);
			intent.putExtra(Constants.EXTRA_URL, "http://www.muqingfeng.cn/forum.php?mobile=2");
			startActivity(intent);
		}
			break;
		case R.id.lingting: {
			Intent intent = new Intent(this, WebViewActivity.class);
			intent.putExtra(Constants.EXTRA_URL, "http://lingting.muqingfeng.cn/");
			startActivity(intent);
		}
			break;
		case R.id.tougao:
			UserTO userTo = AccountManager.getUser(getApplicationContext());
			if (userTo == null) {
				showNOLoginDialog();
			} else {
				startActivity(new Intent(this, SendArticleActivity.class));
			}
			break;
		case R.id.guangjie_d:
			findViewById(R.id.guangjie).performClick();
			// mViewPager.setCurrentItem(0);
			break;
		// case R.id.back:
		// int currentIndex = mViewPager.getCurrentItem();
		// if (currentIndex > 0) {
		// BaseFragment fragment = mViewPagerAdapter.getItem(currentIndex);
		// if (!fragment.onBackPressed()) {
		// mViewPager.setCurrentItem(currentIndex - 1);
		// }
		// } else {
		// finish();
		// }
		// break;
		case R.id.lingting_d:
			findViewById(R.id.lingting).performClick();
			break;
		case R.id.shangcheng_d:
			findViewById(R.id.shangcheng).performClick();
			break;
		case R.id.tougao_d:
			findViewById(R.id.tougao).performClick();
			break;
		case R.id.share:
			ShareSDK.initSDK(this);
			ShareParams sp = new ShareParams();
			sp.setShareType(Wechat.SHARE_WEBPAGE);
			sp.setUrl("http://wap.muqingfeng.cn/forum.php?mod=portal&mobile=2");
			sp.setText("沐青枫-文艺小清新微信社区 分享精彩生活点滴、我们的绚彩世界。");
			sp.setTitle("沐青枫-文艺小清新微信社区 分享精彩生活点滴、我们的绚彩世界。");
			Drawable icon = getResources().getDrawable(R.drawable.ic_launcher);
			sp.setImageData(((BitmapDrawable) icon).getBitmap());
			// sp.setImagePath("/mnt/sdcard/测试分享的图片.jpg");
			Platform weichat = ShareSDK.getPlatform(WechatMoments.NAME);
			weichat.setPlatformActionListener(new PlatformActionListener() {

				@Override
				public void onError(Platform arg0, int arg1, Throwable arg2) {

				}

				@Override
				public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
					Toast.makeText(getApplicationContext(), "分享成功", Toast.LENGTH_SHORT).show();
				}

				@Override
				public void onCancel(Platform arg0, int arg1) {

				}
			}); // 设置分享事件回调
			// 执行图文分享
			weichat.share(sp);
			break;
		case R.id.weixin:
			toPage(1);
			break;
		case R.id.redianxinwen:
			toPage(2);
			break;
		case R.id.tushuotianxia:
			toPage(3);
			break;
		case R.id.gushihui:
			toPage(4);
			break;
		case R.id.lvyouduajia:
			toPage(5);
			break;
		case R.id.xiaoshuoyuedu:
			toPage(6);
			break;
		case R.id.shishangjiaju:
			toPage(7);
			break;
		case R.id.meishifenxiang:
			toPage(8);
			break;
		case R.id.meinvtupian:
			toPage(9);
			break;
		case R.id.qingchunwenxue:
			toPage(10);
			break;
		case R.id.xiaoqingxintu:
			toPage(11);
			break;
		case R.id.xiaohuaqutu:
			toPage(12);
			break;
		case R.id.xingzuowuyu:
			toPage(13);
			break;
		case R.id.weimeihunsha:
			toPage(14);
			break;
		case R.id.shamenshenghuo:
			toPage(15);
			break;
		default:
			break;
		}
	}

	private void toPage(int position) {
		mViewPager.setCurrentItem(position, true);
		mSlidingMenu.toggle();
	}

	private synchronized void showNOLoginDialog() {
		if (mNoLoginDialog != null && mNoLoginDialog.isShowing()) {
			return;
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("提示");
		builder.setMessage("您还未登录，是否立即登录？");
		builder.setNegativeButton("否", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});
		builder.setPositiveButton("是", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = new Intent(MainActivity.this, LoginActivity.class);
				intent.putExtra(Constants.EXTRA_ACTION, LoginActivity.ACTION_RETURN);
				startActivityForResult(intent, REQUEST_CODE_MAIN);
			}
		});
		mNoLoginDialog = builder.create();
		mNoLoginDialog.show();
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != RESULT_OK)
			return;
		UserTO user = AccountManager.getUser(getApplicationContext());
		if (user == null)
			return;
		switch (requestCode) {
		case REQUEST_CODE_MAIN:
			Intent intent = new Intent(this, SendArticleActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}
}
